package protocol.work.connection.http.hct;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;

import protocol.common.util.CJLog;
import protocol.data.util.JedisPoolUtil;
import protocol.work.SystemManager;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class HttpReadDelivrdListener extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected Log logger = CJLog.getLogConsole();
	
	protected String report_pre_fix;
	
	protected JedisPool jedisPool;
	
	public void init() throws ServletException {
		this.jedisPool = JedisPoolUtil.getJedisPool();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Jedis jedis = this.jedisPool.getResource();
		try{
			Map<String,String> reportMap = new HashMap<String,String>();
			String msgId = request.getParameter("src_mobile");
			reportMap.put("DestId",request.getParameter("src_mobile"));
			reportMap.put("SrcTerminalId",request.getParameter("sp_code"));
			reportMap.put("MsgContent", URLEncoder.encode(request.getParameter("msg_content"),"utf-8"));
			reportMap.put("srcnum","0");
			reportMap.put("DataType", "Deliver");
			String mapKey = "Report"+msgId;
			jedis.hmset(mapKey, reportMap);
			jedis.lpush(SystemManager.report_per_fix, mapKey);
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if (jedis != null)
				if (jedis.isConnected())
					this.jedisPool.returnResource(jedis);
				else
					this.jedisPool.returnBrokenResource(jedis);
		}
	}
	

}
